# Copyright (c) 2008
#   Matt Pizzimenti (mjpizz@gmail.com, www.livelearncode.com)
#
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 
# Neither the name of the original author nor the names of contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import utils

from tasks import TaskManager as _TaskManager
from receipts import ReceiptManager as _ReceiptManager

from events import InformationEvent as _InformationEvent
from tossfiles import Tossfile
from users import User
from roles import Role
from flags import Flag

__version__ = "0.0.1"
__author__ = "Matt Pizzimenti"
__author_email__ = "mjpizz@gmail.com"

# THIS CONSITUTES THE PUBLIC API FOR PYTOSS
__all__ = [
    "User",
    "Role",
    "Flag",
    "Tossfile",
    "task",
    "log",
    "local",
    "relpath"
]

local = Role("$")
"""Provides quick access to loggable execution of commands as the local
user on the local machine"""

def task(fn1 = None):
    """Decorator that converts a no-argument function into a pytoss
    Task that can be executed as part of a task stack"""
    if fn1:
        return _TaskManager.get_instance().create_task(task_callback = fn1).execute
    else:
        def wrapper(fn2):
            return _TaskManager.get_instance().create_task(task_callback = fn2).execute
        return wrapper

def log(message):
    """Logs a simple information event to the receipts"""
    _ReceiptManager.get_instance().log_event( _InformationEvent( message ) )

def relpath(path, *additional_path_components):
    """interprets the given path as relative to the CALLER
    of this function, and returns that path as an absolute path"""
    # TODO: check for os.path.isabs(), since this is not necessarily relative anyway
    prefix = utils.absolute_directory_of_caller()
    path = os.path.normpath(path)
    additional_path_components = [os.path.normpath(component) for component in additional_path_components]
    fullpath = os.path.join(prefix, path, *additional_path_components)
    fullpath = os.path.expandvars(fullpath)
    fullpath = os.path.normpath(fullpath)
    return fullpath
